<?php


namespace app\admin\controller;

use app\admin\model\UserModel;
use think\App;
use think\facade\Db\Where;
use think\facade\Db;
use app\BaseController;
use think\facade\View;

class User extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->checkLogin();
        View::assign('left', 'user');
    }
    public function index(){
        $adm = new UserModel();
        $where=[];
        if($name=input('get.name')){
            $where[]=['name','like',"%{$name}%"];
        }
        $where[]=['level','<',2 ];
        $list=$adm->select($where);
        return view('',[
            'list'=>$list,
            'name'=>isset($name)?$name:'',
        ]);
    }
    public function edit(){
        if($id = input('get.')){
            $id = intval($id['id']);
            $list=new UserModel();
            $lst =$list->find($id);

        }else{
            $post=input('post.');
            $where=[];
            if($post['password'] == $post['repassword']){
                $data['password'] = md5('pmc'.$post['password'].'10086');
            }else{
                return $this->error('密码与确认密码不一致','/admin.php/User/edit?id='.$post['id']);
            }
            $data['name']=$post['name'];
//            $data['level']=$post['level'];
            $where=['name'=>$data['name'],'password'=>$data['password']];
            if(!empty($data)){
                Db::name('admin')->where(array('id'=>$post['id']))->update($where);
                $this->success('修改成功','/admin.php/User/index');
            }else{
                $this->error('修改失败','/admin.php/User/edit');
            }
        }
        return view('',[
            'lst'=>$lst,
        ]);
        return view();
    }
    public function add(){
        $oModel=new UserModel();
        if($pos=input('post.')){
            if(empty($pos['name']) || empty($pos['password'])){
                return $this->error('请输入用户名和密码', getenv("HTTP_REFERER"));
            }
            $data['addtime']=time();
            $data['name']=$pos['name'];
            if($pos['password'] == $pos['repassword']){
                $data['password'] = md5('pmc'.$pos['password'].'10086');
            }else{
                return $this->error('密码与确认密码不一致', getenv("HTTP_REFERER"));
            }
//            if($pos['level'] ='管理员'){
//                $data['level'] = 1;
//            }else{
//                $data['level'] = 0;
//            }
            $data['stutic'] =1;

            if( $oModel->add($data)){
                $this->success('1','/admin/User/index');
            }else{
                $this->error('2','/admin/User/add');
            }
        }else{
            return view();
        }

        return view();
    }
    public function del(){
        $id=$_GET['id'];
        $oModel=new UserModel();
        $del=$oModel->del($id);
        return view();
    }
    public function edit_stutic(){
        if(request()->isPost()){
            $post=input('post.');
            $list=Db::name('admin')->where('id',$post['id'])->find();
            if($list['stutic'] == 1){
                $stutic = 2;
                Db::name('admin')->where('id',$post['id'])->update([
                    'stutic'=>$stutic
                ]);
            }else{
                $stutic = 1;
                Db::name('admin')->where('id',$post['id'])->update([
                    'stutic'=>$stutic
                ]);
            }

        }
    }
}